package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.datatransferobje.NoticeAddParam;
import cn.tedu._02notice.pojo.datatransferobje.NoticeListQuery;
import cn.tedu._02notice.pojo.datatransferobje.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.valueobject.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.valueobject.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解：
 * 1.组合注解，相当于@Controller + @ResponseBody；
 * 2.类上添加该在注解后，所有的控制器方法无需再添加@RespenseBody注解了
 * Slf4j注解（记忆：酸辣粉for java）
 * 1.Lombok提供的日志注解
 * 2.添加在类上，会自动生成一个日志对象log
 * 3.log对象的方法
 * 3.1 log.trace("xxx")
 * 3.2 log.debug("xxx")
 * 3.3 log.info("xxx")
 * 3.4 log.warn("xxx")
 * 3.5 log.error("xxx")
 */
@Tag(name = "01-资讯管理里模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice")
//@Controller
public class NoticeController {
    //自动装配
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 1.添加资讯功能
     * <p>
     * 非封装类注释大于封装类，数据类型会给默认的String；
     * 如果参数在方法体上，那么会扫描出数据类型；
     * 没有非封装类注释，则会直接用封装类注释，显示其数据类型
     *
     * @param noticeAddParam
     * @return
     */
//    @Parameters(value = {
//            @Parameter(name = "title", description = "资讯标题", required = true),
//            @Parameter(name = "content", description = "资讯内容", required = true),
//            @Parameter(name = "type", description = "资讯类型", required = true),
//            @Parameter(name = "status", description = "资讯状态", required = true),
//            @Parameter(name = "noticeAddParam", hidden = true)})
    @ApiOperationSupport(order = 10)
    @Operation(summary = "1.添加资讯")
    @PostMapping("/add")//("v1/notice/add")
    //@ResponseBody
    //public String add(NoticeAddParam noticeAddParam) {
    public JsonResult add(NoticeAddParam noticeAddParam) {
        //System.out.println("noticeAddParam:" + noticeAddParam);
        log.debug("noticeAddParam:" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);

        //处理其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice:" + notice);

        noticeMapper.insertNotice(notice);
        //return "添加资讯成功";
        //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "2.资讯列表")
    @PostMapping("list")//("v1/notice/list")
    //@ResponseBody
    //public List<NoticeListVO> query(NoticeListQuery noticeListQuery) {
    public JsonResult query(@RequestBody NoticeListQuery noticeListQuery) {
        log.debug("noticeListQuery:" + noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.queryNotice(noticeListQuery);
        //return noticeListVOS;
        //return new JsonResult(1001,"查询成功",noticeListVOS);
        //return new JsonResult(StatusCode.OPERATION_SUCCESS, noticeListVOS);
        return JsonResult.ok(noticeListVOS);
    }

    @Parameters(value = {@Parameter(name = "id", description = "资讯编号", required = true, example = "1"),
            @Parameter(name = "username", description = "用户名", required = true, hidden = true)})
    //hidden = true，表示隐藏该参数，不会体现到Knife4j文档中
//    @Parameter(name = "id", description = "资讯编号", required = true, example = "1")
//    @Parameter(name = "username", description = "用户名", required = true)
    @ApiOperationSupport(order = 30)
    @Operation(summary = "3.资讯详情")
    @GetMapping("detail")//("/v1/notice/detail")
    //@ResponseBody
    //public NoticeDetailInfoVO queryDetail(Long id) {
    public JsonResult queryDetail(Long id, String username) {
        //username参数没有其他作用，主要是为了Knife4j文档中显示参数
        log.debug("id:" + id);
        NoticeDetailInfoVO noticeDetailInfoVOS = noticeMapper.queryNoticeDetail(id);
        //return noticeDetailInfoVOS;
        //return new JsonResult(StatusCode.OPERATION_SUCCESS, noticeDetailInfoVOS);
        return JsonResult.ok(noticeDetailInfoVOS);
    }

    @ApiOperationSupport(order = 40)
    @Operation(summary = "4.删除资讯")
    @DeleteMapping("del")//("/v1/notice/del")
    //@ResponseBody
    //public String noticeDelete(Long id) {
    public JsonResult noticeDelete(Long id) {
        log.debug("id:" + id);
        int row = noticeMapper.deleteNotice(id);
        if (row > 0) {
            //return "删除公告成功";
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
            return JsonResult.ok();
        } else {
            //return "公告删除失败，资讯不存在";
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @ApiOperationSupport(order = 50)
    @Operation(summary = "5.修改资讯")
    @PostMapping("update")//("/v1/notice/update")
    //@ResponseBody
    //public String noticeUpdate(NoticeUpdateParam noticeUpdateParam) {
    public JsonResult noticeUpdate(NoticeUpdateParam noticeUpdateParam) {
        log.debug("noticeUpdateParam:" + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int row = noticeMapper.updateNotice(notice);
        if (row > 0) {
            //return "更新资讯成功";
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
            return JsonResult.ok();
        } else {
            //return "更新资讯失败";
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
